package com.oes.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oes.dao.AnswerSCPDao;
import com.oes.entity.AnswerSCP;
import com.oes.entity.ExamWithoutTest;
import com.oes.util.DBUtil;

public class AnswerSCPDaoImpl implements AnswerSCPDao {

	public List<AnswerSCP> findByExam(int spid, int eid) {

		List<AnswerSCP> list = new ArrayList<AnswerSCP>();
		AnswerSCP aSCP = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			String sql = "select * from answersinglechoiceproblem where SPid = ? and Eid =?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, spid);
			ps.setInt(2, eid);
			rs = ps.executeQuery();
			while (rs.next()) {
				aSCP = new AnswerSCP();
				aSCP.setAid(rs.getInt("Aid"));
				aSCP.setChoice(rs.getString("Score"));
				aSCP.setScore(rs.getInt("Score"));
				aSCP.setSPid(rs.getInt("SPid"));
				aSCP.setEid(rs.getInt("Eid"));
				list.add(aSCP);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.closeAll(ps, rs, conn);
		}
		return list;
	}

	public int Update(AnswerSCP aSCP) {
		
		int SPid = aSCP.getSPid();
		int Eid = aSCP.getEid();
		int Score = aSCP.getScore();
		String Choice = aSCP.getChoice();
		int Aid = aSCP.getAid();
		
		PreparedStatement ps = null;
		Connection conn = null;
		
		conn = DBUtil.getConnection();
		String sql = "update answersinglechoiceproblem set Score = ?,Choice = ? where SPid = ?"
				+ " and Eid = ? and Aid = ? ";
		int sign = 0;
		
		try {
			
			ps = conn.prepareStatement(sql);
			ps.setInt(1, Score);
			ps.setString(2, Choice);
			ps.setInt(3, SPid);
			ps.setInt(4, Eid);
			ps.setInt(5, Aid);
			
			sign = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtil.close(ps);
			DBUtil.close(conn);
		}

		return sign;
	}

}
